﻿using System;
using System.IO;
using System.Web.Mvc;
using yiCommerce.Service.Pictures;

namespace yiCommerce.Controllers
{
    public class NormalUploaderController : Controller
    {
        private IPictureService _pictureService;

        public NormalUploaderController(IPictureService pictureService)
        {
            _pictureService = pictureService;
        }

        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public JsonResult Upload()
        {

            if (Request.Files.Count > 0)
            {
                try
                {
                    var file = Request.Files[0];

                    var fileName = file.FileName;

                    var fileExtension = Path.GetExtension(fileName);
                    var inputStream = file.InputStream;

                    var contentType = string.Empty;
                    switch (fileExtension)
                    {
                        case ".bmp":
                            contentType = "image/bmp";
                            break;
                        case ".gif":
                            contentType = "image/gif";
                            break;
                        case ".jpeg":
                        case ".jpg":
                        case ".jpe":
                        case ".jfif":
                        case ".pjpeg":
                        case ".pjp":
                            contentType = "image/jpeg";
                            break;
                        case ".png":
                            contentType = "image/png";
                            break;
                        case ".tiff":
                        case ".tif":
                            contentType = "image/tiff";
                            break;
                        default:
                            contentType = "";
                            break;
                    }

                    var dir = @"c:\upload\path";
                    var tempFileName = Guid.NewGuid().ToString() + Path.GetExtension(fileName);

                    var filePath = Path.Combine(dir, tempFileName);

                    var directory = new FileInfo(filePath).Directory;
                    if (directory != null) directory.Create();

                    using (var fs = new FileStream(filePath, FileMode.CreateNew))
                    {

                        inputStream.CopyTo(fs);
                    }

                    var fileBinary = new byte[inputStream.Length];
                    var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);

                    var r = new BinaryReader(fileStream);

                    fileStream.Read(fileBinary, 0, fileBinary.Length);
                    r.BaseStream.Seek(0, SeekOrigin.Begin); //将文件指针设置到文件开

                    fileBinary = r.ReadBytes((int) r.BaseStream.Length);
                    //var memoryStream = new MemoryStream();
                    //upload.InputStream.CopyTo(memoryStream);
                    //var fileBinary= memoryStream.ToArray();




                    var originalPicture = _pictureService.InsertPicture(fileBinary, contentType, null, true);
                    var pictureUrl = _pictureService.GetPictureUrl(originalPicture.Id, 0, true);
                    return Json(new { status = 0, url = pictureUrl }, JsonRequestBehavior.AllowGet);

                }
                catch (Exception e)
                {
                    return Json(new {status = 1, message = e.Message},JsonRequestBehavior.AllowGet);
                    
                }
            }
            else
            {
                return Json(new {status = 1, message = "上传出错:没有文件"},JsonRequestBehavior.AllowGet);
            }


        }

    }
}
